package mbsl;

import mbsl.system.Core;
import mbsl.system.Logger;

/**
 * The main gateway to MBSL.
 * <p>
 * First it verifies any command line arguments, and if they're OK, it starts up a new Core.
 */
public class Main {
	private static Logger logger;
	
	public static void main(String[] pArgs) {
		logger = new Logger(Main.class);
		
		logger.info("Starting MBSL");
		
		if (setup(pArgs))
			new Core();
	}
	
	private static boolean setup(String[] pArguments) {
		for (String argument : pArguments) {
			argument = argument.toLowerCase();
			
			if (argument.startsWith("help")) {
				System.out.println("Valid arguments include:\n" +
						"database.restartThreshold=[threshold > 0]");
			} else if (argument.startsWith("database.restartThreshold=")) {
				try {
					int threshold = Integer.parseInt(argument.substring("database.restartThreshold=".length()));
					
					if (threshold < 1) {
						logger.fatal("database.restartThreshold must be > 0 (default %d)", mbsl.storage.Database.RESTART_THRESHOLD);
						return false;
					} else {
						logger.info("Set database restart threshold to %d", threshold);
						mbsl.storage.Database.RESTART_THRESHOLD = threshold;
					}
				} catch (NumberFormatException e) {}
			}
		}
		
		return true;
	}
}
